package 其他.按照字符数量从小到大输出;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;

public class text {


    public static void main(String[] args) {

        HashMap<Character, Integer> map = new HashMap<>();

        String s = "fjiadfia"; // f 2 j 1 i 2 a 2 d 1      jdfia

        /**
         * 之所以每次遍历这个map 的时候这个 没有按照这个顺序   因为在map 里面并不是按照输入的顺序来的
         */
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)) {
                Integer integer = map.get(c);
                map.put(c, ++integer);
            } else {
                map.put(c, 1);
            }
        }


        TreeSet<Character> set = new TreeSet<>(new Comparator<Character>() {
            @Override
            public int compare(Character o1, Character o2) {
                int diff = map.get(o1) - map.get(o2);
                return diff == 0 ? 1 : diff;
            }
        });

        Set<Character> keySet = map.keySet();
        for (Character character : keySet) {
            set.add(character);
        }

        for (Character character : set) {
            System.out.print(character + ",");
        }
        System.out.println();

        for (Character character : set) {
            System.out.print(map.get(character) + ",");
        }

    }
}
